package gui.catalogues.telephones.models;

import gui.commons.CommonLayoutOperations;
import gui.commons.DialogDataListener;
import gui.commons.IdText;
import gui.commons.ToolbarListener;
import gui.mainframe.MainFrame;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

import org.apache.log4j.Logger;

import model.TelephoneTypes;
import controller.GlobalOperations;
import controller.PersonController;
import controller.TelephoneController;

public class TelephonesDialog extends JDialog {
	/*
	 * logger
	 */
	private final static Logger logger = Logger.getLogger(MainFrame.class);

	/*
	 * field variables declaration
	 */
	private IdText it;
	private String desc;
	private JButton ok, cancel;
	private JTextField tfDescription;
	// private JComboBox<IdText> comboType;
	private CommonLayoutOperations cOps;
	// data listener
	private DialogDataListener ddl;
	// private TelephoneTypesController telephoneTypesController;
	private PersonController personController;
	private TelephoneController telephoneController;

	/*
	 * layout variables declaration
	 */
	private int y;
	private GridBagConstraints gc;
	private Insets lblInset, tfInset, btnInset;
	Border titleBorder = BorderFactory.createTitledBorder("DB connection"),
			spaceBorder = BorderFactory.createEmptyBorder(10, 10, 10, 10);

	/*
	 * TelephoneTypesComboBox
	 */
	TelephoneTypesComboBox telephoneTypesCombo;

	/*
	 * 2013.04.02 telephoneToolbar
	 */
	TelephoneToolbar telephoneToolbar;

	/*
	 * 2013.04.02 globalOperations
	 */
	GlobalOperations gOps;

	public TelephonesDialog(JFrame source, String title, boolean modal) {
		super(source, title, modal);

		/*
		 * init variables and set models
		 */
		initVariables();

		/*
		 * manage all layout
		 */
		manageLayout(source);

		/*
		 * listeners
		 */
		manageListeners();

	}

	public void initVariables() {
		/*
		 * init variables and set models
		 */
		ok = new JButton("Aceptar");
		cancel = new JButton("Cancelar");
		tfDescription = new JTextField(3);
		telephoneToolbar = new TelephoneToolbar();

		// DefaultComboBoxModel typeModel = new DefaultComboBoxModel<IdText>();
		// typeModel.addElement(new IdText(1, "-Escoja uno-"));
		// typeModel.addElement(new IdText(2, "Estático.Local"));
		// typeModel.addElement(new IdText(3, "Estático.Móvil"));
		// typeModel.addElement(new IdText(4, "Estático.Oficina"));

		// comboType = new JComboBox<IdText>(typeModel);

		telephoneTypesCombo = new TelephoneTypesComboBox();

		gOps = new GlobalOperations();
		telephoneController = new TelephoneController();
	}

	public void manageLayout(JFrame source) {

		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		/*
		 * init layout variables and manage it
		 */
		gc = new GridBagConstraints();
		lblInset = new Insets(0, 0, 0, 1);
		tfInset = new Insets(0, 1, 0, 1);
		btnInset = new Insets(1, 1, 1, 1);

		/*
		 * Common tasks variable
		 */
		cOps = new CommonLayoutOperations(lblInset, tfInset, btnInset);

		JPanel controlPanel = new JPanel();
		JPanel buttonsPanel = new JPanel();

		controlPanel.setBorder(BorderFactory.createCompoundBorder(spaceBorder,
				titleBorder));
		// buttonsPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		controlPanel.setLayout(new GridBagLayout());
		buttonsPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

		gc.weightx = 1;
		gc.weighty = 1;
		gc.fill = GridBagConstraints.NONE;

		y = 0;
		cOps.addJLabel(controlPanel, "Tipo: ", 0, y);
		cOps.addJComboBox(controlPanel, telephoneTypesCombo, true, 1, y);
		y++;
		cOps.addJLabel(controlPanel, "Teléfono: ", 0, y);
		cOps.addTextField(controlPanel, tfDescription, 1, y);

		buttonsPanel.add(ok);
		buttonsPanel.add(cancel);
		ok.setPreferredSize(cancel.getPreferredSize());

		/*
		 * finally add the panels to the dialog
		 */
		setLayout(new BorderLayout());
		add(telephoneToolbar, BorderLayout.PAGE_START);
		add(controlPanel, BorderLayout.CENTER);
		add(buttonsPanel, BorderLayout.SOUTH);

		setSize(400, 250);
		setLocationRelativeTo(source);
		Point tmpPoint = getLocation();
		// System.out.println("tmpPoint: " + tmpPoint.toString());
		tmpPoint.y = (int) (tmpPoint.y * .75);
		setLocation(tmpPoint);
	}

	public void manageListeners() {

		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (ddl != null) {
					ddl.saveData();
					// prl.savePreferences(tfUserName.getText(), new String(
					// pfUserPass.getPassword()), (Integer) spPort
					// .getValue());
					// dialogue dialog
				}

				setVisible(false);
			}
		});

		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// System.out.println("diag.vals - port: " + sp.getValue());
				setVisible(false);
			}
		});

		telephoneToolbar.setToolbarListener(new ToolbarListener() {
			@Override
			public void saveItemEventOccured() {
				System.out.println("saveEventOccured - telephoneType: "
						+ telephoneTypesCombo.getSelectedItem()
						+ ", telephoneDescription: " + tfDescription.getText());
			}

			@Override
			public void refreshCollectionEventOccured() {
				System.out.println("refreshEventOccured - telephoneType: "
						+ telephoneTypesCombo.getSelectedItem()
						+ ", telephoneDescription: " + tfDescription.getText());
			}

			@Override
			public void addItemEventOccured() {
				int lastPersonInserted = gOps
						.getIntFromGlobal("lastPersonInserted");
				if (lastPersonInserted != -1) {
					TelephoneTypes tel = (TelephoneTypes) telephoneTypesCombo
							.getSelectedItem();

					telephoneController.addTelephone(tel.getId(),
							lastPersonInserted, tfDescription.getText());
				} else {
					logger.error("Error #0001: No person Id to attach the new telephone to.");
				}
			}

			@Override
			public void updateCollectionEventOccured() {
			}

			@Override
			public void newItemEventOccured() {
				logger.info("'New' button pressed.");
			}
		});
	}

	public void setDialogDataListener(DialogDataListener ddl) {
		this.ddl = ddl;
	}

	// private void connect() {
	// try {
	// telephoneTypesController.connectToDB();
	// } catch (SQLException e) {
	// JOptionPane.showMessageDialog(this,
	// "Cannot connect to the database.", "DB connection problem",
	// JOptionPane.ERROR_MESSAGE);
	// }
	// }

}
